﻿namespace CSharpPro._1103;

using System;

/// <summary>
/// 用户实体类
/// 包含用户的基本信息和业务属性
/// </summary>
public class User
{
    // 字段
    private string username;
    private string password;
    private DateTime registerTime;
    private DateTime lastLoginTime;
    private bool isActive;
    
    // 属性
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username 
    { 
        get => username;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("用户名不能为空");
            username = value.Trim();
        }
    }
    
    /// <summary>
    /// 密码
    /// </summary>
    public string Password 
    { 
        get => password;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("密码不能为空");
            password = value;
        }
    }
    
    /// <summary>
    /// 注册时间
    /// </summary>
    public DateTime RegisterTime 
    { 
        get => registerTime; 
        set => registerTime = value; 
    }
    
    /// <summary>
    /// 最后登录时间
    /// </summary>
    public DateTime LastLoginTime 
    { 
        get => lastLoginTime; 
        set => lastLoginTime = value; 
    }
    
    /// <summary>
    /// 是否激活状态
    /// </summary>
    public bool IsActive 
    { 
        get => isActive; 
        set => isActive = value; 
    }
    
    /// <summary>
    /// 邮箱地址
    /// </summary>
    public string Email { get; set; }
    
    /// <summary>
    /// 手机号码
    /// </summary>
    public string Phone { get; set; }
    
    /// <summary>
    /// 用户角色
    /// </summary>
    public string Role { get; set; }
    
    // 构造函数
    
    /// <summary>
    /// 默认构造函数
    /// </summary>
    public User()
    {
        RegisterTime = DateTime.Now;
        LastLoginTime = DateTime.Now;
        IsActive = true;
        Role = "User"; // 默认角色
    }
    
    /// <summary>
    /// 带用户名和密码的构造函数
    /// </summary>
    /// <param name="username">用户名</param>
    /// <param name="password">密码</param>
    public User(string username, string password) : this()
    {
        Username = username;
        Password = password;
    }
    
    /// <summary>
    /// 完整信息的构造函数
    /// </summary>
    /// <param name="username">用户名</param>
    /// <param name="password">密码</param>
    /// <param name="email">邮箱</param>
    /// <param name="phone">手机号</param>
    /// <param name="role">角色</param>
    public User(string username, string password, string email, string phone, string role) : this(username, password)
    {
        Email = email;
        Phone = phone;
        Role = role;
    }
    
    // 方法
    
    /// <summary>
    /// 更新最后登录时间
    /// </summary>
    public void UpdateLastLoginTime()
    {
        LastLoginTime = DateTime.Now;
    }
    
    /// <summary>
    /// 验证密码是否正确
    /// </summary>
    /// <param name="inputPassword">输入的密码</param>
    /// <returns>是否正确</returns>
    public bool VerifyPassword(string inputPassword)
    {
        return Password == inputPassword;
    }
    
    /// <summary>
    /// 激活用户
    /// </summary>
    public void Activate()
    {
        IsActive = true;
    }
    
    /// <summary>
    /// 禁用用户
    /// </summary>
    public void Deactivate()
    {
        IsActive = false;
    }
    
    /// <summary>
    /// 转换为字符串表示
    /// </summary>
    /// <returns>用户信息的字符串表示</returns>
    public override string ToString()
    {
        return $"用户名: {Username}, 注册时间: {RegisterTime:yyyy-MM-dd HH:mm:ss}, 最后登录: {LastLoginTime:yyyy-MM-dd HH:mm:ss}, 状态: {(IsActive ? "激活" : "禁用")}, 角色: {Role}";
    }
    
    /// <summary>
    /// 获取简化的用户信息
    /// </summary>
    /// <returns>简化信息字符串</returns>
    public string ToSimpleString()
    {
        return $"{Username} ({Role}) - 注册于 {RegisterTime:yyyy-MM-dd}";
    }
    
    /// <summary>
    /// 检查用户是否具有指定角色
    /// </summary>
    /// <param name="role">角色名称</param>
    /// <returns>是否具有该角色</returns>
    public bool HasRole(string role)
    {
        return Role?.Equals(role, StringComparison.OrdinalIgnoreCase) == true;
    }
    
    /// <summary>
    /// 检查用户是否是管理员
    /// </summary>
    /// <returns>是否是管理员</returns>
    public bool IsAdmin()
    {
        return HasRole("Admin") || HasRole("Administrator");
    }
    
    /// <summary>
    /// 获取用户注册天数
    /// </summary>
    /// <returns>注册天数</returns>
    public int GetRegistrationDays()
    {
        return (DateTime.Now - RegisterTime).Days;
    }
    
    /// <summary>
    /// 克隆用户对象（浅拷贝）
    /// </summary>
    /// <returns>克隆的用户对象</returns>
    public User Clone()
    {
        return new User
        {
            Username = this.Username,
            Password = this.Password,
            RegisterTime = this.RegisterTime,
            LastLoginTime = this.LastLoginTime,
            IsActive = this.IsActive,
            Email = this.Email,
            Phone = this.Phone,
            Role = this.Role
        };
    }
    
    /// <summary>
    /// 验证用户对象的有效性
    /// </summary>
    /// <returns>是否有效</returns>
    public bool IsValid()
    {
        return !string.IsNullOrWhiteSpace(Username) && 
               !string.IsNullOrWhiteSpace(Password) &&
               RegisterTime <= DateTime.Now;
    }
}